﻿<?xml version="1.0" encoding="UTF-8"?>

<!-- Device identification transformation for CANopen identification files. -->
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:str="http://xsltsl.org/string" 
	xmlns:ident="x-schema:DTMIdentSchema.xml" 
	xmlns:fdt="x-schema:FDTDataTypesSchema.xml" 
	xmlns:scanident="x-schema:DTMScanIdentSchema.xml"
	xmlns:devident="x-schema:DTMDeviceTypeIdentSchema.xml" 
	xmlns:canopenident="x-schema:FDTCANopenIdentSchema.xml"
	xmlns:canopendevice="x-schema:FDTCANopenDeviceTypeIdentSchema.xml"
	xmlns:canopenscan="x-schema:FDTCANopenScanIdentSchema.xml" version="2.0">

  <!-- Version of this file. -->
  <xsl:variable name="FileVersion">
    <xsl:number value="0.90"/>
  </xsl:variable>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

	<xsl:variable name="newline">
		<xsl:text>
		</xsl:text>
	</xsl:variable>

	<xsl:variable name="newlinetab1">
		<xsl:text>
		</xsl:text>
	</xsl:variable>

	<xsl:variable name="newlinetab2">
		<xsl:text>
			</xsl:text>
	</xsl:variable>

	<xsl:variable name="newlinetab3">
		<xsl:text>
		</xsl:text>
	</xsl:variable>

  <!-- Transform device root or scan identification. -->
  <xsl:template match="/">
    <xsl:apply-templates select="//canopenscan:ScanIdentifications"/>
    <xsl:apply-templates select="//canopendevice:DeviceIdentifications"/>
  </xsl:template>

  <!-- CANopen device identification list. -->
  <xsl:template match="canopendevice:DeviceIdentifications">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
  &lt;!-- This file is created by FDTCANopenIdentTransformation.xsl after transformation of CANopenDeviceIdentInstance.xml. --&gt;
  &lt;FDT xmlns="x-schema:DTMDeviceTypeIdentSchema.xml"
      xmlns:ident="x-schema:DTMIdentSchema.xml" 
      xmlns:fdt="x-schema:FDTDataTypesSchema.xml" &gt;
		</xsl:text>
    <xsl:element name="DeviceIdentifications">
      <xsl:apply-templates select="canopendevice:DeviceIdentification"/>
      <xsl:value-of select="$newlinetab1"/>
    </xsl:element>
    <xsl:value-of select="$newline"/>
    <xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
  </xsl:template>

  <!-- CANopen scan identification list. -->
  <xsl:template match="canopenscan:ScanIdentifications">
    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0"?&gt;
	&lt;!-- This file is created by FDTCANopenIdentTransformation.xsl after transformation of CANopenScanIdentInstance.xml --&gt;
	&lt;FDT xmlns="x-schema:DTMScanIdentSchema.xml" 
			xmlns:ident="x-schema:DTMIdentSchema.xml" 
			xmlns:fdt="x-schema:FDTDataTypesSchema.xml" &gt;
		</xsl:text>
    <xsl:element name="ScanIdentifications">
      <xsl:attribute name="fdt:busCategory">
        <xsl:value-of select="@fdt:busCategory"/>
      </xsl:attribute>
      <xsl:attribute name="resultState">
        <xsl:value-of select="@resultState"/>
      </xsl:attribute>
      <xsl:apply-templates select="canopenscan:ScanIdentification"/>
    </xsl:element>
    <xsl:value-of select="$newline"/>
    <xsl:text disable-output-escaping="yes">&lt;/FDT&gt;</xsl:text>
  </xsl:template>


  <!-- CANopen device identifcation. -->
  <xsl:template match="canopendevice:DeviceIdentification">
    <xsl:value-of select="$newlinetab2"/>
    <xsl:element name="DeviceIdentification">
      <xsl:attribute name="ident:idDTMSupportLevel">
        <xsl:value-of select="@canopenident:idDTMSupportLevel"/>
      </xsl:attribute>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">CANopen</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopendevice:IdBusProtocol/@canopenident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:apply-templates select="canopendevice:IdManufacturer"/>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:apply-templates select="canopendevice:IdTypeID"/>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:apply-templates select="canopendevice:IdSoftwareVersion"/>
      <xsl:value-of select="$newlinetab2"/>
	  
      <xsl:apply-templates select="canopendevice:IdHardwareVersion"/>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdValues">
        <xsl:value-of select="$newlinetab3"/>
        <xsl:apply-templates select="canopendevice:IdProductCode"/>
        <xsl:value-of select="$newlinetab3"/>
        <xsl:apply-templates select="canopendevice:IdRevision"/>
        <xsl:value-of select="$newlinetab2"/>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>
    </xsl:element>
  </xsl:template>

  <!-- CANopen scan identifcation. -->
  <xsl:template match="canopenscan:ScanIdentification">
    <xsl:value-of select="$newlinetab1"/>
    <xsl:element name="ScanIdentification">
      <xsl:value-of select="$newlinetab2"/>

      <xsl:apply-templates select="fdt:CommunicationError"/>

      <xsl:element name="IdBusProtocol">
        <xsl:attribute name="ident:protocolSpecificName">CANopen</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdBusProtocol/@canopenident:busProtocol"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdBusProtocolVersion">
        <xsl:attribute name="ident:protocolSpecificName">not applicable</xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>
      <xsl:element name="IdAddress">
        <xsl:attribute name="ident:protocolSpecificName">Node ID</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdAddress/@canopenident:nodeID"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdManufacturer">
        <xsl:attribute name="ident:protocolSpecificName">Vendor ID</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdManufacturer/@fdt:manufacturerId"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdTypeID">
        <xsl:attribute name="ident:protocolSpecificName">Device type</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdTypeID/@fdt:deviceTypeId"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdSoftwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">Manufacturer software version</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdSoftwareVersion/@fdt:version"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdHardwareRevision">
        <xsl:attribute name="ident:protocolSpecificName">Manufacturer hardware version</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdHardwareVersion/@fdt:version"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdDeviceTag">
        <xsl:attribute name="ident:protocolSpecificName">Manufacturer device name</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdDeviceName/@fdt:name"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdSerialNumber">
        <xsl:attribute name="ident:protocolSpecificName">Serial Number</xsl:attribute>
        <xsl:attribute name="ident:value">
          <xsl:value-of select="canopenscan:IdSerial/@canopenident:serialNumber"/>
        </xsl:attribute>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

      <xsl:element name="IdValues">
        <xsl:apply-templates select="canopenscan:IdProductCode"/>
        <xsl:apply-templates select="canopenscan:IdRevision"/>
      </xsl:element>
      <xsl:value-of select="$newlinetab2"/>

    </xsl:element>
  <xsl:value-of select="$newlinetab1"/>
  </xsl:template>
<xsl:value-of select="$newline"/>

  <!-- Software revision. -->
  <xsl:template match="canopendevice:IdSoftwareVersion">    
    <xsl:param name="value" select="@fdt:version"/>
    <xsl:element name="IdSoftwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">Manufacturer software version</xsl:attribute>
      <xsl:call-template name="genMatch">      
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

 
	<xsl:template match="canopenscan:IdSoftwareVersion">
    <xsl:element name="IdSoftwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">Manufacturer software version</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@canopenscan:IdSoftwareRevision/@fdt:version"/></xsl:attribute>  
    </xsl:element>
  </xsl:template>

  <!-- Hardware revision. -->
  <xsl:template match="canopendevice:IdHardwareVersion">
    <xsl:param name="value" select="@fdt:version"/>
    <xsl:element name="IdHardwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">Manufacturer hardware version</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  
	<xsl:template match="canopenscan:IdHardwareRevision">
    <xsl:element name="IdHardwareRevision">
      <xsl:attribute name="ident:protocolSpecificName">Manufacturer hardware version</xsl:attribute>
      <xsl:attribute name="ident:value"><xsl:value-of select="@canopenscan:IdHardwareVersion/@fdt:version"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- IdRevision -->
  <xsl:template match="canopendevice:IdRevision">
    <xsl:param name="value" select="@canopenident:revisionNumber"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">Revision number</xsl:attribute>
      <xsl:attribute name="ident:name">Revision number</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

  <!-- IdRevision-->
  <xsl:template match="canopenscan:IdRevision">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">Revision number</xsl:attribute>
      <xsl:attribute name="ident:name">Revision number</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@canopenident:revisionNumber"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <!-- IdProductCode -->
  <xsl:template match="canopendevice:IdProductCode">
    <xsl:param name="value" select="@canopenident:productCode"/>
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">Product Code</xsl:attribute>
      <xsl:attribute name="ident:name">Product Code</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>

  <!-- idProductCode-->
  <xsl:template match="canopenscan:IdProductCode">
    <xsl:element name="IdValue">
      <xsl:attribute name="ident:protocolSpecificName">Product Code</xsl:attribute>
      <xsl:attribute name="ident:name">Product Code</xsl:attribute>
      <xsl:attribute name="ident:value">
        <xsl:value-of select="@canopenident:productCode"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>


  <!-- Manufacturer ID. -->
  <xsl:template match="canopendevice:IdManufacturer">
    <xsl:param name="value" select="@fdt:manufacturerId"/>
    <xsl:element name="IdManufacturer">
      <xsl:attribute name="ident:protocolSpecificName">Vendor ID</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  

  <!-- Device type ID. -->
  <xsl:template match="canopendevice:IdTypeID">
    <xsl:param name="value" select="@fdt:deviceTypeId"/>
    <xsl:element name="IdTypeID">
      <xsl:attribute name="ident:protocolSpecificName">Device type</xsl:attribute>
      <xsl:call-template name="genMatch">
        <xsl:with-param name="value" select="$value"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  

  <!-- Communication error. -->
  <xsl:template match="fdt:CommunicationError">
    <xsl:element name="fdt:CommunicationError">
      <xsl:attribute name="communicationError">
        <xsl:value-of select="@communicationError"/>
      </xsl:attribute>
      <xsl:attribute name="tag">
        <xsl:value-of select="@tag"/>
      </xsl:attribute>
    </xsl:element>
    <xsl:value-of select="$newlinetab2"/>
  </xsl:template>

  <!-- Generate matching information. -->
  <xsl:template name="genMatch">
    <xsl:param name="value"/>
    <xsl:param name="empty"/>
    <xsl:if test="$value!=$empty">
      <xsl:attribute name="ident:value">
        <xsl:value-of select="$value"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="canopenident:RegExpr"/>
  </xsl:template>

  <!-- Generate regular exressions. -->
  <xsl:template match="canopenident:RegExpr">
    <!-- Copy pattern info. -->
    <xsl:call-template name="genPattern">
      <xsl:with-param name="match" select="@match"/>
      <xsl:with-param name="nomatch" select="@nomatch"/>
    </xsl:call-template>
  </xsl:template>

  <!-- Generate pattern information. -->
  <xsl:template name="genPattern">
    <xsl:param name="match"/>
    <xsl:param name="nomatch"/>
    <xsl:param name="empty"/>
    <xsl:choose>
      <xsl:when test="$match!=$empty">
        <xsl:element name="ident:RegExpr">
          <xsl:attribute name="match">
            <xsl:value-of select="$match"/>
          </xsl:attribute>
          <xsl:if test="$nomatch!=$empty">
            <xsl:attribute name="nomatch">
              <xsl:value-of select="$nomatch"/>
            </xsl:attribute>
          </xsl:if>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$nomatch!=$empty">
        <xsl:element name="ident:RegExpr">
          <xsl:attribute name="nomatch">
            <xsl:value-of select="$nomatch"/>
          </xsl:attribute>
          <xsl:if test="$match!=$empty">
            <xsl:attribute name="match">
              <xsl:value-of select="$match"/>
            </xsl:attribute>
          </xsl:if>
        </xsl:element>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
